/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import entity.User;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import jpa.UserJpaController;

/**
 *
 * @author riko
 */
public class LoginHelp {

    HttpServletRequest request;

    public LoginHelp(HttpServletRequest request) {
        this.request = request;
    }

    public String login() {
        HttpSession session = request.getSession();
        UserJpaController du = new UserJpaController();
        User user = new User();

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        user.setUsername(username);
        session.setAttribute("temp", user);
        if (username.equals("") || password.equals("")) {
            session.setAttribute("error3", "* Masih ada field yang kosong");
            return "index.jsp";
        } else {
            boolean hasilCheck = du.checkUser(username);
            if (hasilCheck) {
                user = null;
                user = du.getUser(username);
                if (username.equals(user.getUsername()) && password.equals(user.getPassword())) {
                    session.setAttribute("username", user);
                    session.removeAttribute("temp");
                    session.removeAttribute("error");
                    if (user.getAdminStat()) {
                        return "dashboardAdmin.jsp";
                    } else {
                        return "dashboard.jsp";
                    }
                } else {
                    session.setAttribute("error", "gagal login");
                    return "index.jsp";
                }
            } else {
                session.setAttribute("error", "User tidak ditemukan");
                return "index.jsp";
            }
        }

    }
}
